﻿//============================================================
//write by bxl at 2021-06-06 09:45:34
//============================================================



using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.SystemProtocolConfig;
using BCDto.TMS.SystemProtocolConfig;
using BCEntity.TMS.SystemProtocolConfig;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.EntityNameConst;

namespace BCService.TMS.SystemProtocolConfig
{
    public class SystemProtocolConfigService : ISystemProtocolConfigService
    {
        #region var
        private readonly IDatabaseContext database;
        private ISystemProtocolConfigData systemProtocolConfigData;
        private readonly IRedisService redisService;

        private readonly string key = string.Format("{0}", EntityNameConst.SystemProtocolConfigEntity);

        #endregion

        #region 构造函数
        public SystemProtocolConfigService(IRedisService redisService,ISystemProtocolConfigData systemProtocolConfigData, IDatabaseContext databaseContext)
        {
            this.redisService = redisService;
            this.systemProtocolConfigData = systemProtocolConfigData;
            this.database = databaseContext;
        }
        #endregion

        #region Add
        public SystemProtocolConfigDto Add(SystemProtocolConfigRequestDto systemProtocolConfigRequestDto, AdminDto admin)
        {
            var org = GetById(systemProtocolConfigRequestDto.SystemProtocolConfigKey) ;
            if (org != null)
                throw new ArgumentException("系统协议配置Key 已存在");

            var entity = systemProtocolConfigRequestDto.As<SystemProtocolConfigEntity>();
            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminName = admin.FullName;
            entity.CreateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            return systemProtocolConfigData.InsertAsync(entity).GetAwaiter().GetResult().As<SystemProtocolConfigDto>();
        }
        #endregion

        #region update
        public SystemProtocolConfigDto Update(SystemProtocolConfigDto systemProtocolConfigDto, AdminDto admin)
        {
            var entity = systemProtocolConfigData.GetEntityAsync(systemProtocolConfigDto.SystemProtocolConfigKey).GetAwaiter().GetResult() ?? throw new ArgumentException("要修改的记录不存在!");

            entity.ProtocolTitle = systemProtocolConfigDto.ProtocolTitle;
            entity.ProtocolContent = systemProtocolConfigDto.ProtocolContent;

            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            this.redisService.DeleteMultipleKey(key, false);
            return systemProtocolConfigData.UpdateAsync(entity).GetAwaiter().GetResult().As<SystemProtocolConfigDto>();

        }
        #endregion

        #region Delete
        public void Delete(string systemProtocolConfigKey)
        {
            this.redisService.DeleteMultipleKey(key, false);
            systemProtocolConfigData.DeleteAsync(systemProtocolConfigKey).GetAwaiter().GetResult();

        }
        #endregion

        #region GetBySystemProtocolConfigKey
        public SystemProtocolConfigDto GetById(string systemProtocolConfigKey)
        {
            string cacheKey = this.redisService.GetKey(key, systemProtocolConfigKey);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = systemProtocolConfigData.GetEntityAsync(systemProtocolConfigKey).GetAwaiter().GetResult();
                return a.As<SystemProtocolConfigDto>();
            }
            , TimeSpan.FromDays(1));
            return result;
        }
        #endregion

        #region GetList  获取系统协议配置记录--[分页]
        public PageableList<SystemProtocolConfigDto> GetList(string protocolTitle, string protocolContent, int pageIndex, int pageSize)
        {

            string cacheKey = this.redisService.GetKey(key, protocolTitle, protocolContent, pageIndex, pageSize);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                int fCount = 0;
                IEnumerable<SystemProtocolConfigEntity> fList;

                (fList, fCount) = systemProtocolConfigData.GetListAsync(protocolTitle, protocolContent, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<SystemProtocolConfigDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<SystemProtocolConfigDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}